<template>
  <div class="register-container">
    <div class="register-content">
      <div class="brand-section">
        <div class="logo-container">
          <img :src="require(`@/assets/img/logo.png`)" class="logo-image" alt="融销通">
        </div>
        <div class="brand-title">
          <h1 class="main-title">融销通</h1>
          <div class="sub-title">农产品融销一体化平台</div>
        </div>
      </div>
      
      <div class="form-container">
        <div class="form-header">
          <h2>用户注册</h2>
          <div class="form-subtitle">创建您的账号，开始使用融销通服务</div>
        </div>
        
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-position="top" class="register-form">
          <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username"  placeholder="请输入账号" autocomplete="off"></el-input>
          </el-form-item>
          
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="ruleForm.nickname"  placeholder="请输入昵称" autocomplete="off"></el-input>
          </el-form-item>
          
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password"  placeholder="请输入密码" autocomplete="off"></el-input>
          </el-form-item>
          
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass"  placeholder="请再次输入密码" autocomplete="off"></el-input>
          </el-form-item>
          
          <div class="form-actions">
            <el-button type="primary" round @click="register('ruleForm')" class="submit-btn">
              <i class="el-icon-check"></i> 注册
            </el-button>
            <el-button round @click="resetForm('ruleForm')" class="reset-btn">
              <i class="el-icon-refresh"></i> 重置
            </el-button>
          </div>
          
          <div class="form-footer">
            <el-link type="primary" @click="$router.push('/login');">已有账号？去登录</el-link>
          </div>
        </el-form>
      </div>
    </div>
    
  </div>
</template>

<script>
import {userLogin, userRegister} from "../api/user";

export default {
  name: 'Register',

  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    };
    var validateNickname = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入昵称'));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username:'',
        password: '',
        checkPass: '',
        nickname:'',
      },
      rules: {
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        username:[
          { validator: validateUsername, trigger: 'blur' }
        ],
        nickname:[
          { validator: validateNickname, trigger: 'blur' }
        ]
      }
    };
  },

  methods:{
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    register(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm.username);
          userRegister({
            userName: this.ruleForm.username,
            password: this.ruleForm.password,
            nickName: this.ruleForm.nickname,
            avatar: "rongxiaotong.gif",
          })
            .then((res) => {
              if (res.flag == true) {
                this.$message.success(res.message);
                this.$store.commit("updateLoginUserNickname", this.nickName);
                this.$router.push("/login").catch((err) => err);
              } else {
                this.$message.error(res.data);
              }
            })
            .catch((err) => {
              this.$message.error(err);
            });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style lang="less" scoped>
.register-container {
  position: relative;
  height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.register-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url("../assets/img/Login.jpg") center center;
  background-size: cover;
  filter: brightness(0.4) blur(2px);
  z-index: -1;
}

.register-content {
  width: 100%;
  max-width: 1000px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2rem;
  z-index: 1;
}

.brand-section {
  text-align: center;
  margin-bottom: 2rem;
  
  .logo-container {
    margin-bottom: 1rem;
    
    .logo-image {
      width: 120px;
      height: auto;
      filter: drop-shadow(0 0 10px rgba(255, 170, 0, 0.5));
    }
  }
  
  .brand-title {
    .main-title {
      font-size: 3.5rem;
      font-weight: 700;
      color: #ffaa00;
      margin: 0;
      text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
      letter-spacing: 2px;
    }
    
    .sub-title {
      font-size: 1.5rem;
      color: #ff9f34;
      margin-top: 0.5rem;
      text-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
    }
  }
}

.form-container {
  background: rgba(26, 26, 26, 0.8);
  border-radius: 10px;
  padding: 2.5rem;
  width: 100%;
  max-width: 500px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  
  .form-header {
    text-align: center;
    margin-bottom: 2rem;
    
    h2 {
      font-size: 1.8rem;
      color: #ffffff;
      margin: 0;
      font-weight: 600;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    }
    
    .form-subtitle {
      color: var(--text-secondary);
      margin-top: 0.5rem;
      font-size: 1rem;
    }
  }
  
  .register-form {
    .el-form-item__label {
      color: var(--text-primary);
      font-weight: 500;
      padding-bottom: 0.5rem;
    }
    
    .el-input__inner {
      background-color: rgba(42, 42, 42, 0.8);
      border: 1px solid var(--border-color);
      color: var(--text-primary);
      height: 45px;
      border-radius: 6px;
      font-size: 1rem;
      
      &:focus {
        border-color: #dba155;
        box-shadow: 0 0 0 2px rgba(219, 161, 85, 0.2);
      }
      
      &::placeholder {
        color: rgba(204, 204, 204, 0.6);
      }
    }
    
    .el-input__prefix {
      color: #dba155;
    }
  }
  
  .form-actions {
    display: flex;
    gap: 1rem;
    margin-top: 1.5rem;
    
    .submit-btn, .reset-btn {
      flex: 1;
      height: 45px;
      font-size: 1rem;
      font-weight: 500;
    }
    
    .submit-btn {
      background: linear-gradient(135deg, #dba155, #ffaa00);
      border-color: #dba155;
      color: #121212;
      
      &:hover {
        background: linear-gradient(135deg, #e3b06c, #ffb733);
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(219, 161, 85, 0.4);
      }
    }
    
    .reset-btn {
      background-color: rgba(255, 255, 255, 0.1);
      border-color: rgba(219, 161, 85, 0.5);
      color: #dba155;
      
      &:hover {
        background-color: rgba(255, 255, 255, 0.2);
        color: #e3b06c;
      }
    }
  }
  
  .form-footer {
    text-align: center;
    margin-top: 1.5rem;
    
    .el-link {
      color: #dba155;
      font-size: 0.95rem;
      
      &:hover {
        color: #ffaa00;
        text-shadow: 0 0 5px rgba(219, 161, 85, 0.5);
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .register-content {
    padding: 1.5rem;
  }
  
  .brand-section {
    .brand-title {
      .main-title {
        font-size: 2.5rem;
      }
      
      .sub-title {
        font-size: 1.2rem;
      }
    }
  }
  
  .form-container {
    padding: 1.5rem;
  }
}
</style>
